//commData.js
import React, { Component } from 'react';
import PropTypes from 'prop-types';
/**
 * 单元文本内容
 */
export default class CommData extends Component {
    constructor(props) {
        super(props)
        this.state = {
            // text:this.props.valueObj.text,  //文本
            // dataType:"",
            // note:""
            //IsThousandsFormat: 0 // 默认
        }
       // this.display = this.determineDisplayText({ x: props.x, y: props.y }, this.props.valueObj.text)
        this.display = this.determineDisplayText({ x: props.x, y: props.y }, this.props.valueObj,
            this.props.isThousandsFormat,
            this.props.isPercentageFormat,
            this.props.decimalPlace
        )

    }


     //5.四舍五入保留2位小数（不够位数，则用0替补）
              keepTwoDecimalFull=(num)=> {
                  var result = parseFloat(num);
                      if (isNaN(result)) {
                              alert('传递参数错误，请检查！');
                              return false;
                          }
                      result = Math.round(num * 100) / 100;
                      var s_x = result.toString(); //将数字转换为字符串

                      var pos_decimal = s_x.indexOf('.'); //小数点的索引值


                      // 当整数时，pos_decimal=-1 自动补0
                      if (pos_decimal < 0) {
                              pos_decimal = s_x.length;
                              s_x += '.';
                          }

                      // 当数字的长度< 小数点索引+2时，补0
                      while (s_x.length <= pos_decimal + 2) {
                              s_x += '0';
                          }
                      return s_x;
                 }


    //mum数字 n是位置
    keepNDecimalFull=(num,n)=> {
        var result = parseFloat(num);
        if (isNaN(result)) {
            alert('传递参数错误，请检查！');
            return false;
        }
        if(n==0){
            result = Math.round(num );
            var s_x = result.toString(); //将数字转换为字符串
            return s_x;
        }else if(n==1){
            result = Math.round(num * 10) / 10;
            var s_x = result.toString(); //将数字转换为字符串
            var pos_decimal = s_x.indexOf('.'); //小数点的索引值
            // 当整数时，pos_decimal=-1 自动补0
            if (pos_decimal < 0) {
                pos_decimal = s_x.length;
                s_x += '.';
            }
            // 当数字的长度< 小数点索引+2时，补0
            while (s_x.length <= pos_decimal + 1) {
                s_x += '0';
            }
            return s_x;
        }else if(n==2){
            result = Math.round(num * 100) / 100;
            var s_x = result.toString(); //将数字转换为字符串
            var pos_decimal = s_x.indexOf('.'); //小数点的索引值
            // 当整数时，pos_decimal=-1 自动补0
            if (pos_decimal < 0) {
                pos_decimal = s_x.length;
                s_x += '.';
            }
            // 当数字的长度< 小数点索引+2时，补0
            while (s_x.length <= pos_decimal + 2) {
                s_x += '0';
            }
            return s_x;
        }else if(n==3){
            result = Math.round(num * 1000) / 1000;
            var s_x = result.toString(); //将数字转换为字符串
            var pos_decimal = s_x.indexOf('.'); //小数点的索引值
            // 当整数时，pos_decimal=-1 自动补0
            if (pos_decimal < 0) {
                pos_decimal = s_x.length;
                s_x += '.';
            }
            // 当数字的长度< 小数点索引+2时，补0
            while (s_x.length <= pos_decimal + 3) {
                s_x += '0';
            }
            return s_x;
        }else if(n==4){
            result = Math.round(num * 10000) / 10000;
            var s_x = result.toString(); //将数字转换为字符串
            var pos_decimal = s_x.indexOf('.'); //小数点的索引值
            // 当整数时，pos_decimal=-1 自动补0
            if (pos_decimal < 0) {
                pos_decimal = s_x.length;
                s_x += '.';
            }
            // 当数字的长度< 小数点索引+2时，补0
            while (s_x.length <= pos_decimal + 4) {
                s_x += '0';
            }
            return s_x;
        }else if(n==5){
            result = Math.round(num * 100000) / 100000;
            var s_x = result.toString(); //将数字转换为字符串
            var pos_decimal = s_x.indexOf('.'); //小数点的索引值
            // 当整数时，pos_decimal=-1 自动补0
            if (pos_decimal < 0) {
                pos_decimal = s_x.length;
                s_x += '.';
            }
            // 当数字的长度< 小数点索引+2时，补0
            while (s_x.length <= pos_decimal + 5) {
                s_x += '0';
            }
            return s_x;
        }

    }



    /**
     * Before updating, execute the formula on the Cell value to calculate the
     * `display` value. Especially useful when a redraw is pushed upon this cell
     * when editing another cell that this might depend upon
     */
    componentWillUpdate() {
        //this.display =this.determineDisplayText({ x: this.props.x, y: this.props.y }, this.props.valueObj.text)
        this.display =this.determineDisplayText({ x: this.props.x, y: this.props.y }, this.props.valueObj,
            this.props.isThousandsFormat,this.props.isPercentageFormat,this.props.decimalPlace)
    }
    /**
     * Executes the formula calculation on the cell value
     */
    determineDisplayText = ({ x, y }, valueText,isThousandsFormat,isPercentageFormat,
                            decimalPlace) => {
        //todo  decimalPlace
        let _valueText=valueText;
        if (valueText.slice(0, 1) === '=') {
            const res = this.props.executeFormula({ x, y }, valueText.slice(1))
            if (res.error !== null) {
                return 'INVALID'
            }
            _valueText=res.result;
            _valueText=_valueText.toString();
            console.log('++++++++++++++++++++++++++++++++=')
            console.log((res.result.toString()));
            if(!isNaN(_valueText)  && _valueText.trim().length>0){   //非数字 则说明都不做
                _valueText=this.keepTwoDecimalFull(_valueText)
              // _valueText=_valueText.replace(/(\d)(?=(\d{3})+(?:\.\d+)?$)/g, "$1,");
                if(isThousandsFormat==1){        //  千分位
                    _valueText=_valueText.replace(/(\d)(?=(\d{3})+(?:\.\d+)?$)/g, "$1,");
                }else if(isPercentageFormat==1){
                    //percent  format  %
                    _valueText=_valueText * 100 + '%'
                }
              }
            return _valueText
        }

        //非公式的情况
        if(!isNaN(_valueText)  && _valueText.trim().length>0){   //非数字 则说明都不做
            //_valueText=this.keepTwoDecimalFull(_valueText)
            //console.log("_valueText:"+_valueText);
            //console.log("isThousandsFormat:"+isThousandsFormat+";isThousandsFormat==1:"+isThousandsFormat==1)
            //todo
            let  _d=decimalPlace;
            if(!isNaN(_d)){
               _d=parseInt(_d);
                if(_d>=0){  //进行小数格式化
                    _valueText=this.keepNDecimalFull(_valueText,_d);
                }
            }

            if(isThousandsFormat==1){        //  千分位
                _valueText=_valueText.replace(/(\d)(?=(\d{3})+(?:\.\d+)?$)/g, "$1,");
            }else if(isPercentageFormat==1){
                //percent  format  %
                _valueText=_valueText * 100 + '%'
            }
            //_valueText=this.thousandsFilter(_valueText);
        }
        return _valueText
    }


    render() {
        return (
            <span>{this.display}</span>
            // <span>{this.props.valueObj.text}</span>
        )
    }
}

